// 大写字符串首字母
function upperHead(str){
	var head = str.charAt(0).toUpperCase();
	return head + str.substr(1);
}
//处理键盘事件 禁止后退键（Backspace）密码或单行、多行文本框除外
function banBackSpace(e){
	var ev = e || window.event;// 获取event对象
	var obj = ev.target || ev.srcElement;// 获取事件源
	var t = obj.type || obj.getAttribute('type');// 获取事件源类型
	// 获取作为判断条件的事件类型
	var vReadOnly = obj.getAttribute('readonly');
	var vEnabled = obj.getAttribute('enabled');
	// 处理null值情况
	vReadOnly = (vReadOnly == null) ? false : vReadOnly;
	vEnabled = (vEnabled == null) ? true : vEnabled;
	// 当敲Backspace键时，事件源类型为密码或单行、多行文本的，
	// 并且readonly属性为true或enabled属性为false的，则退格键失效
	var flag1=(ev.keyCode == 8 && (t=="password" || t=="text" || t=="textarea") && (vReadOnly || vEnabled!=true))?true:false;
	// 当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效
	var flag2=(ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea")?true:false;
	// 判断
	if(flag2){
		return false;
	}
	if(flag1){
		return false;
	}
}

// 禁止后退键 作用于Firefox、Opera
document.onkeypress=banBackSpace;
// 禁止后退键 作用于IE、Chrome
document.onkeydown=banBackSpace;
/**
 * 时间对象的格式化;
 */
Date.prototype.format = function(format) {
    /*
     * eg:format="yyyy-MM-dd hh:mm:ss";
     */
    var o = {
        "M+" : this.getMonth() + 1, // month
        "d+" : this.getDate(), // day
        "h+" : this.getHours(), // hour
        "m+" : this.getMinutes(), // minute
        "s+" : this.getSeconds(), // second
        "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
        "S" : this.getMilliseconds()
        // millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
                        - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[k]
                 :("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

/**
 * 时间字符串转时间
 */
function yMdhfm2msec(yMdhfm){
	if(yMdhfm){
		return new Date(yMdhfm.replace(/-/g,"/")).getTime();
	}
	return -1;
}

function ajaxErrorDeal(request, textStatus, errorThrown){
    // "timeout", "error", "notmodified" 和 "parsererror"
    // 通常 textStatus 和 errorThrown 之中
    // 只有一个会包含信息
    //this; // 调用本次AJAX请求时传递的options参数
    if('timeout' == textStatus){
        alertModel('访问超时');
    }else if('error' == textStatus){
        switch (request.status){
            case 0: return;
            case 401:;
            case 403:;
            case 410:;
            case 404:;
            case 500:alertModel(request.responseJSON.message);return;
        }
        alertModel('错误代码：' + request.status +"\n"+textStatus +"\n"+errorThrown);
    }else if('parsererror' == textStatus){
        alertModel('解析异常');
    }
}

$.ajaxSetup({
    async: true,
    cache: false,
    /*beforeSend: function(){
    	$.blockUI({message: '\
    		<div class="spinner">\
    		</div>'});
    },*/
    /*complete: function(){
    	$.unblockUI();
    },*/
    error: function (request, textStatus, errorThrown) {
    	ajaxErrorDeal(request, textStatus, errorThrown);
    },
    global: false,
    type: "POST"
});


function alertModel(warnInfo,funName,args){
    var content = '\
    <div class="modal fade" style="z-index:2000" id="_frm_warn_modal" tabindex="-1" role="dialog" aria-labelledby="_frm_warn_modal_label">\
      <div class="modal-dialog" role="document">                                                    \
        <div class="modal-content">                                                                 \
          <div class="modal-header">                                                                \
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">            \
                <span aria-hidden="true">&times;</span>                                             \
            </button>                                                                               \
            <h4 class="modal-title" id="_frm_warn_modal_label">提示</h4>                            \
          </div>                                                                                    \
          <div class="modal-body" id="_frm_warn_modal_body">                                        \
          </div>                                                                                    \
          <div class="modal-footer">                                                                \
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>        \
          </div>                                                                                    \
        </div>                                                                                      \
      </div>                                                                                        \
    </div>';
    //if($('#_frm_warn_modal').size() == 0){
    $('body').append(content);
    //}
    $('#_frm_warn_modal').off().on('hidden.bs.modal', function (e) {
    	$('#_frm_warn_modal').remove();
    	if(funName)
    		funName(args);
    });
    $('#_frm_warn_modal_body').html(warnInfo);
    $('#_frm_warn_modal').modal('show');
}

function confirmModel(confirmInfo,funName,args){
    var content = '\
    <div class="modal fade" style="z-index:2000" id="_frm_confirm_modal" tabindex="-1" role="dialog" aria-labelledby="_frm_confirm_modal_label">\
      <div class="modal-dialog" role="document">                                                    \
        <div class="modal-content">                                                                 \
          <div class="modal-header">                                                                \
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">            \
                <span aria-hidden="true">&times;</span>                                             \
            </button>                                                                               \
            <h4 class="modal-title" id="_frm_confirm_modal_label">提示</h4>                            \
          </div>                                                                                    \
          <div class="modal-body" id="_frm_confirm_modal_body">                                        \
          </div>                                                                                    \
          <div class="modal-footer">                                                                \
    		<button type="button" class="btn btn-default"  id="_frm_confirm_modal_yes" >确定</button>        \
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>        \
          </div>                                                                                    \
        </div>                                                                                      \
      </div>                                                                                        \
    </div>';
    if($('#_frm_confirm_modal').size() == 0){
    	$('body').append(content);
    }
    $('#_frm_confirm_modal').off().on('hidden.bs.modal', function (e) {
    	$('#_frm_confirm_modal').remove();
    });
    $('#_frm_confirm_modal_yes').off().on("click", function(){
    	$('#_frm_confirm_modal').modal('hide');
		funName(args);
	});

    $('#_frm_confirm_modal_body').html(confirmInfo);
    $('#_frm_confirm_modal').modal('show');
}

function confirmModelStatus(confirmInfo,funName,args){
    var content = '\
    <div class="modal fade" style="z-index:2000" id="_frm_confirm_modal_status" tabindex="-1" role="dialog" aria-labelledby="_frm_confirm_modal_status_label">\
      <div class="modal-dialog" role="document">                                                    \
        <div class="modal-content">                                                                 \
          <div class="modal-header">                                                                \
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">            \
                <span aria-hidden="true">&times;</span>                                             \
            </button>                                                                               \
            <h4 class="modal-title" id="_frm_confirm_modal_status_label">提示</h4>                            \
          </div>                                                                                    \
          <div class="modal-body" id="_frm_confirm_modal_status_body">                                        \
          </div>                                                                                    \
          <div class="modal-footer">                                                                \
    		<button type="button" class="btn btn-default"  id="_frm_confirm_modal_status_yes" >确定</button>        \
            <button type="button" class="btn btn-default" id="_frm_confirm_modal_status_no">关闭</button>        \
          </div>                                                                                    \
        </div>                                                                                      \
      </div>                                                                                        \
    </div>';
    if($('#_frm_confirm_modal_status').size() == 0){
    	$('body').append(content);
    }
    $('#_frm_confirm_modal_status').off().on('hidden.bs.modal', function (e) {
    	$('#_frm_confirm_modal_status').remove();
    });
    $('#_frm_confirm_modal_status_yes').off().on("click", function(){
    	$('#_frm_confirm_modal_status').modal('hide');
		funName(true,args);
	});
    $('#_frm_confirm_modal_status_no').off().on("click", function(){
    	$('#_frm_confirm_modal_status').modal('hide');
		funName(false,args);
	});

    $('#_frm_confirm_modal_status_body').html(confirmInfo);
    $('#_frm_confirm_modal_status').modal('show');
}

function alertBlock(warnInfo){
	var content = '\
		<div id="_frm_confirm_block_alert" style=" cursor: default;z-index:2000;"> \
			<div>提示</div>\
	        <div id="_frm_confirm_block_alert_info"></div>\
	        <div><a class="btn btn-default" onclick="$.unblockUI();">关闭</a></div>\
	    </div>';
	$.blockUI({ onOverlayClick: $.unblockUI,message: content });
//	$('body').append(content);
	$('#_frm_confirm_block_alert_info').html(warnInfo);

}

function confirmBlock(confirmInfo,funName,args){
	var content = '\
		<div id="_frm_confirm_block" class="modal_alert" style=" cursor: default;z-index:2000;"> \
			<div>提示</div>\
	        <div id="_frm_confirm_block_info"></div>\
	        <div><a class="btn-alert btn-info col-sm-6" id="_frm_block_confirm_yes">确定</a>\
		<a class="btn-alert btn-default col-sm-6" id="_frm_block_confirm_no">关闭</a></div>\
	    </div>';
//	if($('#_frm_confirm_block').size() == 0){
//		$('body').append(content);
//	}
	$.blockUI({ onOverlayClick: function(){$.unblockUI},message: content });
	$('#_frm_confirm_block_info').html(confirmInfo);
	$('#_frm_block_confirm_yes').off().on("click", function(){
		$.unblockUI();
		funName(args);
	});
	$('#_frm_block_confirm_no').off().on("click", function(){
		$.unblockUI();
	});
}

function confirmBlockStatus(confirmInfo,funName,args){
	var content = '\
		<div id="_frm_confirm_status_block" class="modal_alert" style=" cursor: default;z-index:2000;"> \
			<div>提示</div>\
	        <div id="_frm_confirm_status_block_info"></div>\
	        <div><a class="btn-alert btn-info col-sm-6" id="_frm_confirm_status_block_yes">确定</a>\
		<a class="btn-alert btn-default col-sm-6" id="_frm_confirm_status_block_no">关闭</a></div>\
	    </div>';
	$.blockUI({ onOverlayClick: $.unblockUI,message: content });
//	if($('#_frm_confirm_status_block').size() == 0){
//		$('body').append(content);
//	}
	$('#_frm_confirm_status_block_info').html(confirmInfo);
	$('#_frm_confirm_status_block_yes').off().on("click", function(){
		$.unblockUI();
		funName(true,args);
	});
	$('#_frm_confirm_status_block_no').off().on("click", function(){
		$.unblockUI();
		funName(false,args);
	});

}
